package com.control;

import android.util.Log;

import com.bean.LoginInfo;
import com.bean.UserInfo;
import com.xike.util.Manager;
import com.xike.util.StringUtil;

public class UserControl {
	public static boolean isLogined() {
		return StringUtil.isNotNull(getAccessToken()) && StringUtil.isNotNull(getAccessTokenSecret());
	}

	public static void logout() {
		Manager.appInfoSpEditor.putString("access_token", "").commit();
		Manager.appInfoSpEditor.putString("access_token_secret", "").commit();

		Manager.appInfoSpEditor.putString("im_username", "").commit();
		Manager.appInfoSpEditor.putString("im_pwd", "").commit();

		Manager.appInfoSpEditor.putLong("user_id", 0).commit();
	}

	public static void saveLoginInfo(LoginInfo loginInfo) {
		Manager.appInfoSpEditor.putString("access_token", loginInfo.getAccess_token()).commit();
		Manager.appInfoSpEditor.putString("access_token_secret", loginInfo.getAccess_token_secret()).commit();

		Manager.appInfoSpEditor.putString("im_username", loginInfo.getIm_username()).commit();
		Manager.appInfoSpEditor.putString("im_pwd", loginInfo.getIm_pwd()).commit();

		if (loginInfo.getUser() != null) {
			updateUserInfo(loginInfo.getUser());
		}
	}

	public static void updateUserInfo(UserInfo user) {
		Manager.appInfoSpEditor.putLong("user_id", user.getId()).commit();
		Manager.appInfoSpEditor.putString("user_name", user.getShow_name()).commit();
		Manager.appInfoSpEditor.putString("user_avator", user.getAvator()).commit();

		Manager.appInfoSpEditor.putBoolean("is_pay", user.isIs_pay()).commit();
		Manager.appInfoSpEditor.putBoolean("is_bind_mobile", user.isIs_bind_mobile()).commit();

		saveLoginUserBindMobile(user.getMobile());
	}

	public static String getLoginUserAvator() {
		return Manager.appInfoSp.getString("user_avator", "");
	}

	public static void saveLoginUserBindMobile(String mobile) {
		Manager.appInfoSpEditor.putString("mobile", mobile).commit();
	}

	public static String getLoginUserBindMobile() {
		return Manager.appInfoSp.getString("mobile", "");
	}

	public static boolean hasBindMobile() {
		return Manager.appInfoSp.getBoolean("is_bind_mobile", false);
	}

	public static boolean isPayUser() {
		return Manager.appInfoSp.getBoolean("is_pay", false);
	}

	public static void setIsPayUser(boolean isPay) {
		Log.e("setIsPayUser", "setIsPayUser");
		Manager.appInfoSpEditor.putBoolean("is_pay", isPay).commit();
	}

	public static String getAccessToken() {
		return Manager.appInfoSp.getString("access_token", "");
	}

	public static void updateLoginUserName(String userName) {
		Manager.appInfoSpEditor.putString("user_name", userName).commit();
	}

	public static String getLoginUserName() {
		return Manager.appInfoSp.getString("user_name", "");
	}

	public static String getAccessTokenSecret() {
		return Manager.appInfoSp.getString("access_token_secret", "");
	}

	public static long getLoginUserId() {
		return Manager.appInfoSp.getLong("user_id", 0);
	}

	public static String getImUserName() {
		return Manager.appInfoSp.getString("im_username", "");
	}

	public static String getImPwd() {
		return Manager.appInfoSp.getString("im_pwd", "");
	}

	// public static void setUserInfoChanged(boolean change) {
	// Manager.appInfoSpEditor.putBoolean("userInfoChange", change);
	// }
	//
	// public static boolean isUserInfoChanged() {
	// return Manager.appInfoSp.getBoolean("userInfoChange", false);
	// }

}
